package com.su.lightthings.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.navigation.findNavController
import com.su.lightthings.R

class CustomTitleBar(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {

    private lateinit var tvTitle: TextView
    private lateinit var ivFunc: ImageView
//    var onBackPressed: (() -> Unit)? = null

    init {
        loadView()
    }

    @SuppressLint("MissingInflatedId")
    private fun loadView() {
        val view = LayoutInflater.from(context).inflate(R.layout.title_bar, this)
        tvTitle = view.findViewById(R.id.tv_title)
        view.findViewById<ImageView>(R.id.iv_title_bar_back).setOnClickListener {
            this.findNavController().popBackStack()
        }
        ivFunc = view.findViewById<ImageView>(R.id.iv_title_bar_function)
        ivFunc.setOnClickListener {
            functionClickListener?.invoke()
        }
    }

    var functionClickListener: (() -> Unit)? = null
    fun setTitle(title: String) = run { tvTitle.text = title }
    fun setFunctionViewVisible(visible: Boolean) {
        ivFunc.visibility = if (visible) View.VISIBLE else View.GONE
    }

}